package cn.com.chb.dakele.util;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import cn.com.chb.dakele.domain.QianDaoContent;
import cn.com.chb.dakele.domain.ReplayContent;
import cn.com.chb.dakele.net.Http;

public class IKFenci {
	private static final Log log = LogFactory.getLog(Dakele.class);
	public static Map<String, Integer> getTextDef(String text) throws IOException {
		Map<String, Integer> wordsFren = new HashMap<String, Integer>();
		IKSegmenter ikSegmenter = new IKSegmenter(new StringReader(text), true);
		Lexeme lexeme;
		while ((lexeme = ikSegmenter.next()) != null) {
			if (lexeme.getLexemeText().length() > 1) {
				if (wordsFren.containsKey(lexeme.getLexemeText())) {
					wordsFren.put(lexeme.getLexemeText(), wordsFren.get(lexeme
							.getLexemeText()) + 1);
				} else {
					wordsFren.put(lexeme.getLexemeText(), 1);
				}
			}
		}
		return wordsFren;
	}

	public static String sortSegmentResult(Map<String, Integer> wordsFrenMaps,int topWordsCount) {
//		Iterator<Map.Entry<String, Integer>> wordsFrenMapsIterator = wordsFrenMaps.entrySet().iterator();
//		while (wordsFrenMapsIterator.hasNext()) {
//			Map.Entry<String, Integer> wordsFrenEntry = wordsFrenMapsIterator.next();
////			System.out.println(wordsFrenEntry.getKey() + "|的次数为:"+ wordsFrenEntry.getValue());
//			//log.info(wordsFrenEntry.getKey() + ":"+ wordsFrenEntry.getValue());
//		}

		List<Map.Entry<String, Integer>> wordFrenList = new ArrayList<Map.Entry<String, Integer>>(wordsFrenMaps.entrySet());
		Collections.sort(wordFrenList,
				new Comparator<Map.Entry<String, Integer>>() {
					public int compare(Map.Entry<String, Integer> obj1,
							Map.Entry<String, Integer> obj2) {
						return obj2.getValue() - obj1.getValue();
					}
				});
		String afterSort = "";
		for (int i = 0; i < topWordsCount && i < wordFrenList.size(); i++) {
			Map.Entry<String, Integer> wordFrenEntry = wordFrenList.get(i);
			if (wordFrenEntry.getValue() > 1) {
			//	log.info(wordFrenEntry.getKey() + "："+ wordFrenEntry.getValue());
				afterSort+=wordFrenEntry.getKey();
			}
		}
		return afterSort;
	}

	public static void main(String args[]) throws IOException, InterruptedException {
		int topWordsCount = 5;
		Dakele dakele = new Dakele();
		dakele.login();
		QianDaoContent qd = dakele.sign("http://bbs.dakele.com/dsu_paulsign-sign.html");
		//System.out.println(qd.getFormhash());
		//System.out.println(qd.getFormAction());
//		for(int i=7;i<200;i++){
//			String basurl="http://bbs.dakele.com/forum-43-"+i+".html";
//			log.info(basurl);
//			List<String> urls = dakele.getThreadURLs(basurl);
//			for(String url:urls){
//				log.info(url);
//				ReplayContent content = dakele.preReplay(url);
//				if(content!=null){
//					//log.info(content.getUrl());
//					//log.info(content.getMessage());
//					Map<String, Integer> wordsFrenMaps = getTextDef(content.getMessage());
//					String me = sortSegmentResult(wordsFrenMaps, topWordsCount);
//					//BasicCookieStore co = new BasicCookieStore();
//					//Http.setCookieStore(co);
//					if(me.length()>=5){
//						content.setMessage(me);
//						//dakele.replay(content);
//						log.info(content.getUrl());
//						log.info(content.getMessage());
//						//Thread.sleep(16000);
//					}
//				}
//			}
//		}
		

	}
}
